package com.lms.controller;

import com.alibaba.fastjson.JSON;
import com.lms.entity.Admin;
import com.lms.mapper.AdminMapper;
import com.lms.service.AdminService;
import com.lms.vo.DataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: 管理员控制器类
 * @author: L-B-S
 * @Date: 2021/3/3 21:32
 * @modified_By:
 * @Version: 0.0$
 */
@Controller
public class AdminController {

    Admin admin = new Admin();
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;

    @ResponseBody
    @PostMapping(value = "/admin/login")
    public DataVo<Admin> login(String username, String password, HttpSession session, Model model) {
        DataVo<Admin> dataVo = adminService.login(username, password);
        if (dataVo.getCode() == 0) {
            Admin admin = adminMapper.selectByUserName(username);
            model.addAttribute("loginMsg", admin);
            session.setAttribute("loginInfo", admin);
        }
        return dataVo;
    }

    @ResponseBody
    @PostMapping(value = "/admin/register")
    public DataVo<Admin> register(Admin admin) {
        return adminService.register(admin);
    }

    @ResponseBody
    @GetMapping(value = "/admin/getInfo")
    public DataVo<Admin> adminInfo(HttpSession session, Integer page, Integer limit) {
        Admin loginInfo = (Admin) session.getAttribute("loginInfo");
        return adminService.adminInfo(loginInfo, page, limit);
    }

    @GetMapping(value = "/admin/quit")
    public String quit(HttpSession session) {
        session.removeAttribute("loginInfo");
        return "redirect:/";
    }

    @ResponseBody
    @PostMapping(value = "/admin/sendMsg")
    public DataVo<Admin> sendMsg(String username, String phone, HttpSession session) {
        String[] codeInfo = new String[3];
        codeInfo[0] = username;
        codeInfo[1] = phone;
        session.setAttribute("codeInfo", codeInfo);
        admin.setUserName(username);
        admin.setPhone(phone);
        return adminService.sendMsg(admin, session);
    }

    @ResponseBody
    @PostMapping(value = "/admin/verify")
    public DataVo<Admin> verifyCode(String code, HttpSession session) {
        return adminService.verify(code, session);
    }

    @ResponseBody
    @PostMapping(value = "/admin/resetPwd")
    public DataVo<Admin> resetPwd(String password, HttpSession session) {
        return adminService.resetPwd(password, session);
    }

    @ResponseBody
    @GetMapping(value = "/admin/getList")
    public DataVo<Admin> getList(Integer page, Integer limit, HttpSession session, Model model) {
        Object info = session.getAttribute("loginInfo");
        model.addAttribute("userInfo", info);
        return adminService.getList(page, limit);
    }

    public DataVo<Admin> delAdmins(@RequestParam(value = "adminList") String adminList) {
        List<Admin> data = JSON.parseArray(adminList, Admin.class);
        return adminService.delAdmins(data);
    }

    @GetMapping(value = "/admin/{url}")
    public String redirect(@PathVariable("url") String url) {
        return "admin/" + url;
    }
}
